package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heim.file.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.USerThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private WmMaterialMapper wmMaterialMapper;
@Autowired
private MinIoTemplate minIoTemplate;
    @Override
    public ResponseResult login(MultipartFile multipartFile) throws IOException {
        //获取上传图片的输入流
        InputStream inputStream = multipartFile.getInputStream();

        //文件类型  image/png
        //根据/进行分割，获取/后面的内容
        String contentType = multipartFile.getContentType();
        if (StringUtils.isEmpty(contentType)){
            log.warn("图片格式错误");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);
        }
        //不为空就进行分割（解析图片类型）
        String[] split = contentType.split("/");//数组是{image，png}
        String suffixName = split[1];//通过获取数组后面的那位获取后缀名



        //把图片上传minio
        //第一个填写的是网址前缀， 文件的名称  输入流 类型（图片）
        String url = minIoTemplate.uploadFile("", System.currentTimeMillis() + "."+suffixName, inputStream, "image");

        //将上传的url写入到数据库material表
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId(USerThreadLocal.get().getId());  //组装wmMaterial数据
        wmMaterial.setUrl(url);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setType((short)0);
        wmMaterial.setCreatedTime(new Date());
        int insertResult = wmMaterialMapper.insert(wmMaterial);
        if (insertResult <1){
            log.warn("文件上传失败");
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }


        //将创建好的素材返回给前端
        return ResponseResult.okResult(wmMaterial);


    }



    @Override
    public PageResponseResult list(WmMaterialDto dto) {

        //入参判断
if (dto==null){
    return new PageResponseResult( 0,0,0);
}
     //构造器的特性，前面可以加条件
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
     //isCollection=1时才生效
     wrapper.eq(dto.getIsCollection()==1,WmMaterial::getIsCollection,dto.getIsCollection());

       //分页条件
        Integer dtoPage = dto.getPage();//页码
        Integer size = dto.getSize(); //每页显示条数
        Page<WmMaterial> page = new Page<>(dtoPage,size );
        //selectPageResult不会为空  分页查询
        Page selectPageResult = wmMaterialMapper.selectPage(page, wrapper);

        //返回值结构
        Long total = selectPageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(dtoPage,size,total.intValue());
        pageResponseResult.setData(selectPageResult.getRecords());//获取数据

        return pageResponseResult;
    }
}
